package com.sh.data.engine.web.openapi.controller.datasource;

import com.sh.data.engine.application.model.dto.base.PagerResponse;
import com.sh.data.engine.application.model.dto.integration.DataSourceDTO;
import com.sh.data.engine.application.model.dto.integration.DataSourceDetailDTO;
import com.sh.data.engine.application.model.request.integration.DataSourceCreateRequest;
import com.sh.data.engine.application.model.request.integration.DataSourceGeneralConnectionRequest;
import com.sh.data.engine.application.service.integration.DataSourceApplicationService;
import com.sh.data.engine.common.annotation.crypto.EncryptMethod;
import com.sh.data.engine.common.enumDefinition.ProjectStatisticTypeEnum;
import com.sh.data.engine.domain.annotation.LogExecution;
import com.sh.data.engine.domain.annotation.ProjectStatisticExecution;
import com.sh.data.engine.domain.annotation.userlog.Business;
import com.sh.data.engine.domain.annotation.userlog.EngineOperation;
import com.sh.data.engine.web.webapi.controller.base.response.RestResponseEntity;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


@Validated
@RestController
@RequestMapping("/openapi/datasource")
@Tag(name = "数据源", description = "数据源相关接口")
public class DataSourceOpenController {

    @Autowired
    private DataSourceApplicationService dataSourceApplicationService;

    @Operation(summary = "更新数据源", description = "支持更新多种类型的数据源，包括RDBMS、Hive、Kafka、Mqtt、MongoDB、HBase、HDFS、Dgraph、IoTDB、InfluxDB、Opentsdb等")
    @PostMapping("/updateDataSource")
    @LogExecution(operation = EngineOperation.UPDATE, business = Business.数据源)
    public RestResponseEntity<Boolean> updateDataSource(
            @RequestBody DataSourceCreateRequest request) {
        if (request.getId() == null) {
            return RestResponseEntity.error(400, "数据源Id不能为空");
        }
        var result = dataSourceApplicationService.updateDataSource(request);
        return RestResponseEntity.success(result);
    }

    @Operation(summary = "创建数据源", description = "支持创建多种类型的数据源，包括RDBMS、Hive、Kafka、Mqtt、MongoDB、HBase、HDFS、Dgraph、IoTDB、InfluxDB、Opentsdb等")
    @PostMapping("/createDataSource")
    @EncryptMethod
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.REGISTER_DS)
    @LogExecution(operation = EngineOperation.ADD, business = Business.数据源)
    public RestResponseEntity<Boolean> createDataSource(
            @RequestBody DataSourceCreateRequest request) {
        var result = dataSourceApplicationService.createDataSource(request);
        return RestResponseEntity.success(result);
    }

    @PostMapping("/deleteDataSource")
    @Operation(summary = "删除数据源")
    @ProjectStatisticExecution(type = ProjectStatisticTypeEnum.REGISTER_DS)
    @LogExecution(operation = EngineOperation.DEL, business = Business.数据源)
    public RestResponseEntity<Boolean> deleteDataSourceById(Long id) {
        var result = dataSourceApplicationService.dropDataSourceById(id);
        return new RestResponseEntity<>(result);
    }

    @PostMapping("/testDataSourceConnectivity")
    @Operation(summary = "测试数据源连接性")
    @EncryptMethod
    @LogExecution(operation = EngineOperation.START_TASK, business = Business.数据源)
    public RestResponseEntity<Boolean> testRdbmsConnection(
            @RequestBody @Valid DataSourceGeneralConnectionRequest request) {
        var result = dataSourceApplicationService.testDataSource(request);
        return new RestResponseEntity<>(result);
    }

    @GetMapping("/listDataSources")
    @Operation(summary = "获取数据源列表")
    public RestResponseEntity<PagerResponse<DataSourceDTO>> getDSPageList(
            @Parameter(description = "数据源类型") @RequestParam(required = false) String dsType,
            @Parameter(description = "数据源名称") @RequestParam(required = false) String dsName,
            @Parameter(description = "连接状态 0-失败 1-成功") @RequestParam(required = false) Integer status,
            @Parameter(description = "当前页", schema = @Schema(defaultValue = "1")) @RequestParam(required = false, defaultValue = "1") Integer pageNum,
            @Parameter(description = "每页大小", schema = @Schema(defaultValue = "10")) @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        var result = dataSourceApplicationService.getDataSourcePageList(dsType, dsName, status, pageNum, pageSize);
        return RestResponseEntity.success(result);
    }

    @GetMapping("/getDataSource")
    @Operation(summary = "获取数据源详情")
    public RestResponseEntity<DataSourceDetailDTO> getDataSourceDetailById(Long id) {
        var result = dataSourceApplicationService.getDataSourceDetailById(id);
        return new RestResponseEntity<>(result);
    }
}
